package com.tbit.uqbike.webmanager.util.http;


import lombok.extern.slf4j.Slf4j;
import org.apache.http.conn.ssl.SSLSocketFactory;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;

@Slf4j
public class MySSLSocketFactory extends SSLSocketFactory {

    private static MySSLSocketFactory mySSLSocketFactory = null;

    static {
        mySSLSocketFactory = new MySSLSocketFactory(createSContext());
    }


    private MySSLSocketFactory(SSLContext sslContext) {
        super(sslContext);
        this.setHostnameVerifier(ALLOW_ALL_HOSTNAME_VERIFIER);
    }

    private static SSLContext createSContext() {
        SSLContext sslcontext = null;
        try {
            sslcontext = SSLContext.getInstance("SSL");
        } catch (NoSuchAlgorithmException e) {
            log.error(e.getMessage(), e);
        }
        try {
            sslcontext.init(null, new TrustManager[]{new TrustAnyTrustManager()}, null);
        } catch (KeyManagementException e) {
            log.error(e.getMessage(), e);
            return null;
        }
        return sslcontext;
    }

    public static MySSLSocketFactory getInstance() {
        if (mySSLSocketFactory != null) {
            return mySSLSocketFactory;
        } else {
            return mySSLSocketFactory = new MySSLSocketFactory(createSContext());
        }
    }

}